---
title: 消息接收监听
hide_title: true
sidebar_position: 3
---

<Tabs
groupId="sdks-language"
values={[
{ label: 'Android', value: 'andorid', },
{ label: 'iOS', value: 'ios', },
{ label: 'JavaScript', value: 'js', }
]
}>
<TabItem value="andorid">

```java
JetIM.getInstance().getMessageManager().addListener("main", new IMessageManager.IMessageListener() {
    @Override
    public void onMessageReceive(Message message) {
        Log.d("TAG", "onMessageReceive, conversationType is " + message.getConversation().getConversationType() + ", conversationId is " + message.getConversation().getConversationId());
        MessageContent content = message.getContent();
        if (content instanceof TextMessage) {
            Log.d("TAG", "text did receive, content is " + ((TextMessage) content).getContent());
        } else if (content instanceof ImageMessage) {
            Log.d("TAG", "image did receive, url is " + ((ImageMessage) content).getUrl());
        } else if (content instanceof FileMessage) {
            Log.d("TAG", "file did receive, name is " + ((FileMessage) content).getName());
        } else if (content instanceof VoiceMessage) {
            Log.d("TAG", "voice did receive");
        }
    }
});
```


</TabItem>
<TabItem value="ios">

全局只需设置一次，多次设置会被覆盖。

```objc
[JIM.shared.messageManager setDelegate:self];
- (void)messageDidReceive:(JMessage *)message {
    NSLog(@"messageDidReceive conversationType is %d, conversationId is %@", message.conversation.conversationType, message.conversation.conversationId);
    JMessageContent *content = message.content;
    if ([content isKindOfClass:[JTextMessage class]]) {
        NSLog(@"text messageDidReceive, content is %@", ((JTextMessage *)content).content);
    } else if ([content isKindOfClass:[JImageMessage class]]) {
        NSLog(@"image messageDidReceive, url is %@", ((JImageMessage *)content).url);
    } else if ([content isKindOfClass:[JFileMessage class]]) {
        NSLog(@"file messageDidReceive");
    } else if ([content isKindOfClass:[JVoiceMessage class]]) {
        NSLog(@"voice messageDidReceive");
    }
}
```
</TabItem>
<TabItem value="js">

全局只需设置一次，多次设置会覆盖，其他人给当前用户发送消息会触发消息监听，消息格式请查看 [Message 结构](../msg/web.mdx)，Event 说明请查看 [监听枚举](../enum/web#listener)

```js
let { Event } = jetim;

jetim.on(Event.MESSAGE_RECEIVED, (message) => {
  console.log(message);
});
```
</TabItem>
</Tabs>